/*
 * HistoricalDataFetcher.java
 *
 * Created on January 13, 2008, 11:36 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fie.datareader;


import com.fie.calculations.Volatility;
import java.net.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class HistoricalDataFetcher {
    
    private Volatility v;
    
    public HistoricalDataFetcher(String ticker) throws Exception {
        
        //Currently 6 months of historical prices work the best.
        URL yahoo = new URL("http://ichart.finance.yahoo.com/table.csv?s="+ticker+"&a=11&b=06&c=2007&d=00&e=12&f=2008&g=d&ignore=.csv");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        List<String> historicalData = new ArrayList();
        
        while ((inputLine = in.readLine()) != null) 
            historicalData.add(inputLine);
        in.close();
        
        
        for(int i = 1; i<historicalData.size();i++)
            System.out.println(historicalData.get(i));
        
        
      System.out.println(historicalData.size());
     
      String[] cols = new String[7]; 
      
      List<String[]> dataToProc = new ArrayList();
      
      for(int j = 1; j<historicalData.size();j++){
            cols = historicalData.get(j).split(",");
            String[] tmp = { cols[4],cols[4],cols[4] };
            dataToProc.add(tmp);
        
      }
      
      
      for(int k=0;k<dataToProc.size();k++){
          System.out.println(dataToProc.get(k)[2]);
      }
      
      
       v = new Volatility(dataToProc);
      
        
    }//End of method
    
    public String getVolatility(){
        return ""+(v.getVolatility()*100);
    }
    
}
